#!/usr/bin/env bash
# SPDX-License-Identifier: BSD-3-Clause
# Copyright 2018-2019, Intel Corporation
#
#
# pmempool_sync/TEST31 -- test for sync command with badblocks
#                         - bad blocks in the dax device
#                           blocks: offset: 1000 length: 8
#

. ../unittest/unittest.sh

require_test_type medium
require_build_type debug nondebug

require_badblock_tests_enabled dax_device
require_linked_with_ndctl $PMEMPOOL$EXESUFFIX

setup

. ../common_badblock.sh

badblock_test_init dax_device

LOG=out${UNITTEST_NUM}.log
rm -f $LOG && touch $LOG

POOLSET=$DIR/testset1
create_poolset $POOLSET AUTO:$FULLDEV:x \
			R \
			8M:$DIR/testfile1:z

expect_normal_exit $PMEMPOOL$EXESUFFIX rm $POOLSET
expect_normal_exit $PMEMPOOL$EXESUFFIX create obj --layout pmempool$SUFFIX $POOLSET

expect_normal_exit "$OBJ_VERIFY$EXESUFFIX $POOLSET pmempool$SUFFIX c v &>> $LOG"

turn_on_checking_bad_blocks $POOLSET

# inject bad block: OFF=1000 LEN=8
ndctl_inject_error $NAMESPACE 1000 8

expect_bad_blocks $NAMESPACE

expect_normal_exit "$PMEMPOOL$EXESUFFIX info --bad-blocks=yes $POOLSET >> $LOG"

expect_bad_blocks $NAMESPACE

expect_abnormal_exit "$OBJ_VERIFY$EXESUFFIX $POOLSET pmempool$SUFFIX v" > /dev/null 2>&1

expect_bad_blocks $NAMESPACE

expect_normal_exit "$PMEMPOOL$EXESUFFIX sync -b -v $POOLSET >> $LOG"

print_bad_blocks $NAMESPACE

expect_normal_exit "$PMEMPOOL$EXESUFFIX check -v $POOLSET >> $LOG"

print_bad_blocks $NAMESPACE

expect_normal_exit "$PMEMPOOL$EXESUFFIX info --bad-blocks=yes $POOLSET >> $LOG"

print_bad_blocks $NAMESPACE

expect_normal_exit "$OBJ_VERIFY$EXESUFFIX $POOLSET pmempool$SUFFIX v &>> $LOG"

ndctl_uninject_error $FULLDEV $NAMESPACE 1000 8

badblock_test_fini

check

pass
